package edu.bistu.sim.xwy.blog.Controller;
import edu.bistu.sim.xwy.blog.Service.BusUserService;
import edu.bistu.sim.xwy.blog.Service.NewsUserService;
import edu.bistu.sim.xwy.blog.domain.BusUser;
import edu.bistu.sim.xwy.blog.domain.NewsInfo;
import edu.bistu.sim.xwy.blog.domain.NewsUser;
import net.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang3.RandomStringUtils;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/bus")
public class BusUserController {
    @Autowired
    private BusUserService busUserService;
    @GetMapping("/findAll")
    public List<BusUser> findAll(){
        List <BusUser> busUsers =new ArrayList<>();
        List<BusUser> bus1=busUserService.findAll();
        for (BusUser busUser : bus1){
            if (busUser.getBus_user_desc()==null){
                System.out.println("商家信息未注册，不予显示");

            }else{
                busUsers.add(busUser);
            }
        }
        return busUsers;
    }
    @RequestMapping("/loginConfirm")
    public BusUser loginConfirm(@Param("username")String username,@Param("password")String password){
        return busUserService.loginConfirm(username, password);
    }
    @RequestMapping("/addBus")
    public BusUser addUser(@Param("username")String username,@Param("password") String password){
        BusUser busUser = new BusUser();
        busUser.setBus_user_password(password);
        busUser.setBus_user_name(username);
        return busUserService.save(busUser);
    }
    @RequestMapping("/resetInfo")
    public BusUser resetInfo(@Param("user_name")String user_name,@Param("user_tel")String user_tel,@Param("user_gender")String user_gender,@Param("user_mail")String user_mail,@Param("user_work")String user_work){

        BusUser busUser = busUserService.getOne(busUserService.findUserByName(user_tel).getBus_user_id());
        busUser.setBus_user_name(user_name);
        busUser.setBus_user_tel(user_tel);
        busUser.setBus_user_mail(user_mail);
        busUser.setBus_user_sex(user_gender);
        busUserService.save(busUser);
        return busUser;
    }
//    @RequestMapping("/findByName")
//    public BusUser findByName(@Param("user_name")String user_name){
//        System.out.println("用户名为: "+user_name);
//        return busUserService.findUserByName(user_name);
//    }
    @RequestMapping("/findByName")
    public BusUser findByName(@Param("user_name") String user_name) {
        System.out.println("用户名为: " + user_name);
        BusUser busUser = busUserService.findUserByName(user_name);
        System.out.println(busUser.getBus_user_name());
        System.out.println(busUser.getBus_user_id());
        if (busUser != null) {
            // 对返回的BusUser对象进行进一步操作
            String userName = busUser.getBus_user_name();
            System.out.println("用户名为1: " + userName);
            // 可根据需要构建响应实体，并将BusUser对象作为响应体返回
            return busUser;
        }else{
            System.out.println("失败");
            return busUser;
        }

    }
}
